/*
 *
 *  Connection Manager
 *
 *  Script to send DBUS message to flimflam to announce changes in the
 *  state of a ppp connection.
 *
 *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dbus/dbus.h>

#define PPPD_INTERFACE "org.samba.pppd"
#define PPPD_PATH "/org/sambda/pppd"

static const char * keys [] = {
	"DEVICE", "device",
	"DNS1", "dns1",
	"DNS2", "dns2",
	"PPP_IFACE", "interface",
	"PPP_LOCAL", "iplocal",
	"PPP_REMOTE", "ipremote",
	"PPP_TTY", "tty",
	"SPEED", "speed"
};

static void append(DBusMessageIter *dict,
		   const char *key,
		   const char *value)
{
	DBusMessageIter entry;

	dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
							NULL, &entry);

	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);

	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);

	dbus_message_iter_close_container(dict, &entry);
}

int main(int argc, char *argv[])
{
	DBusConnection *conn;
	DBusError error;
	DBusMessage *msg;
	DBusMessageIter iter, dict;
	dbus_uint32_t pid;
	const char *pid_string;
	char *busname;
	int ii;
	char *endptr;

	busname = getenv("PPP_IPPARAM");
	if (busname == NULL) {
		busname = "org.chromium.flimflam";
	}

	pid_string = getenv("PPPD_PID");
	if (pid_string == NULL) {
		fprintf(stderr, "%s: PPPD_PID must be in env\n", argv[0]);
		exit(-1);
	}
	pid = strtol(pid_string, &endptr, 10);
	if (endptr == pid_string) {
		fprintf(stderr, "%s: Invalid value for PPPD_PID '%s'\n",
			argv[0], pid_string);
		exit(-1);
	}

	dbus_error_init(&error);

	conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
	if (conn == NULL) {
		if (dbus_error_is_set(&error) == TRUE) {
			fprintf(stderr, "%s\n", error.message);
			dbus_error_free(&error);
		} else
			fprintf(stderr, "Failed to get on system bus\n");
		return 0;
	}

	msg = dbus_message_new_method_call(busname, PPPD_PATH,
					   PPPD_INTERFACE, "Notify");
	if (msg == NULL) {
		dbus_connection_unref(conn);
		fprintf(stderr, "Failed to allocate method call\n");
		return 0;
	}

	dbus_message_set_no_reply(msg, TRUE);

	dbus_message_append_args(msg, DBUS_TYPE_UINT32, &pid,
				DBUS_TYPE_INVALID);

	dbus_message_iter_init_append(msg, &iter);

	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
			DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
			DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);

	for (ii = 0; ii < sizeof(keys)/sizeof(const char *); ii += 2) {
		const char *value = getenv(keys[ii]);

		if (value) {
			append(&dict, keys[ii+1], value);
		}
	}

	dbus_message_iter_close_container(&iter, &dict);

	if (dbus_connection_send(conn, msg, NULL) == FALSE)
		fprintf(stderr, "Failed to send message\n");

	dbus_connection_flush(conn);

	dbus_message_unref(msg);

	dbus_connection_unref(conn);

	return 0;
}
